(function($) {
  var btn = $(".btn-pause");
  var indicator = $(".carousel-indicators li");
  
  $(".btn-pause").click(function() {
    var paused = btn.hasClass("paused");
    var playing = btn.hasClass("playing");
  
    if (playing) {
      $("#carousel-hero.carousel").carousel("pause");
      btn
        .children(".fas.fa-pause")
        .removeClass("fa-pause")
        .addClass("fa-play");
      btn.removeClass("playing");
      btn.addClass("paused");
    }
  
    if (paused) {
      $("#carousel-hero.carousel").carousel("cycle");
      btn
        .children(".fas.fa-play")
        .removeClass("fa-play")
        .addClass("fa-pause");
      btn.removeClass("paused");
      btn.addClass("playing");
    }
  });
  
  $(".carousel-indicators li").on("click", function() {
    var pause = indicator.hasClass("active");
  
    if (pause) {
      $("#carousel-hero.carousel").carousel("pause");
      btn.children(".fas.fa-pause")
        .removeClass("fa-pause")
        .addClass("fa-play");
      btn.removeClass("playing");
      btn.addClass("paused");
    }
  });
  
  $.fn.bcSwipe = function(settings) {
    var config = { threshold: 50 };
    if (settings) {
      $.extend(config, settings);
    }
  
    this.each(function() {
      var stillMoving = false;
      var start;
  
      if ("ontouchstart" in document.documentElement) {
        this.addEventListener("touchstart", onTouchStart, false);
      }
  
      function onTouchStart(e) {
        if (e.touches.length == 1) {
          start = e.touches[0].pageX;
          stillMoving = true;
          this.addEventListener("touchmove", onTouchMove, false);
        }
      }
  
      function onTouchMove(e) {
        if (stillMoving) {
          var x = e.touches[0].pageX;
          var difference = start - x;
          if (Math.abs(difference) >= config.threshold) {
            cancelTouch();
            if (difference > 0) {
              $(this).carousel("next")
                .carousel("pause");
              btn.children(".fas.fa-pause")
                .removeClass("fa-pause")
                .addClass("fa-play");
              btn.removeClass("playing");
              btn.addClass("paused");
            } else {
              $(this)
                .carousel("prev")
                .carousel("pause");
              btn.children(".fas.fa-pause")
                .removeClass("fa-pause")
                .addClass("fa-play");
              btn.removeClass("playing");
              btn.addClass("paused");
            }
          }
        }
      }
  
      function cancelTouch() {
        this.removeEventListener("touchmove", onTouchMove);
        start = null;
        stillMoving = false;
      }
    });
  
    return this;
  };
  
  // Swipe functions for Bootstrap Carousel
  $("#carousel-hero.carousel").bcSwipe({ threshold: 50 });
  
})(jQuery);
